package nc.ui.scm.plugin;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import nc.ui.ic.pub.query.QueryDlgUtil;
import nc.ui.pub.ButtonObject;
import nc.ui.pub.bill.BillEditEvent;
import nc.ui.pub.bill.BillItemEvent;
import nc.ui.pub.bill.BillMouseEnent;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.scm.plugin.Action;
import nc.vo.scm.pub.SCMEnv;

public class InvokeEventProxy
{
  private ArrayList<IScmUIPlugin> listPlugin;
  private String cbilltype;
  private IUIPluginToftPanel toftpanel;
  private String prodcode;
  private SCMUIContext ctx;
  private String uiclassname;
  private boolean inited = false;

  public InvokeEventProxy(String prodcode, String cbilltype, IUIPluginToftPanel toftpanel)
  {
    this(prodcode, cbilltype, null, toftpanel);
  }

  public InvokeEventProxy(String prodcode, String cbilltype, String ui_classname, IUIPluginToftPanel toftpanel)
  {
    this.cbilltype = cbilltype;
    this.prodcode = prodcode;
    this.toftpanel = toftpanel;
    this.uiclassname = ui_classname;
  }

  private void init()
  {
    if (this.inited) return; try
    {
      this.listPlugin = UIPluginFactory.getIntance().getExtUIPlugins(this.prodcode, this.cbilltype, this.uiclassname, getSCMUIContext());
    } catch (Throwable e) {
      SCMEnv.error(e);
    }
    this.inited = true;
  }

  public void beforeButtonClicked(ButtonObject bo)
    throws BusinessException
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.beforeButtonClicked(bo, getSCMUIContext());
  }

  public void afterButtonClicked(ButtonObject bo)
    throws BusinessException
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.afterButtonClicked(bo, getSCMUIContext());
  }

  public SCMUIContext getSCMUIContext()
  {
    if (this.ctx == null)
      this.ctx = new SCMUIContext(this.prodcode, this.cbilltype, this.toftpanel);
    return this.ctx;
  }

  public boolean ifUsePlugin()
  {
    return (this.listPlugin != null) && (this.listPlugin.size() > 0);
  }

  public boolean beforeEdit(BillEditEvent e)
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return true;
    boolean bret = true;
    for (IScmUIPlugin pl : this.listPlugin) {
      if (!pl.beforeEdit(e, getSCMUIContext()))
        bret = false;
    }
    return bret;
  }

  public boolean beforeEdit(BillItemEvent e)
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return true;
    boolean bret = true;
    for (IScmUIPlugin pl : this.listPlugin) {
      if (!pl.beforeEdit(e, getSCMUIContext()))
        bret = false;
    }
    return bret;
  }

  public void afterEdit(BillEditEvent e) {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.afterEdit(e, getSCMUIContext());
  }

  public void bodyRowChange(BillEditEvent e)
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.bodyRowChange(e, getSCMUIContext());
  }

  public void mouse_doubleclick(BillMouseEnent e)
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.mouse_doubleclick(e, getSCMUIContext());
  }

  public void onMenuItemClick(ActionEvent e)
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.onMenuItemClick(e, getSCMUIContext());
  }

  public void beforeAction(Action action, AggregatedValueObject[] billvos) throws BusinessException
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.beforeAction(action, billvos, getSCMUIContext());
  }

  public void afterAction(Action action, AggregatedValueObject[] billvos) throws BusinessException
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.afterAction(action, billvos, getSCMUIContext());
  }

  public String getCbilltype()
  {
    return this.cbilltype;
  }

  public void setCbilltype(String cbilltype)
  {
    this.cbilltype = cbilltype;
  }

  public String getProdcode() {
    return this.prodcode;
  }

  public void setProdcode(String prodcode) {
    this.prodcode = prodcode;
  }

  public IUIPluginToftPanel getToftpanel() {
    return this.toftpanel;
  }

  public void setToftpanel(IUIPluginToftPanel toftpanel) {
    this.toftpanel = toftpanel;
  }

  public void setButtonStatus()
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.setButtonStatus(getSCMUIContext());
  }

  public void beforeSetBillVOToCard(AggregatedValueObject billvo)
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.beforeSetBillVOToCard(billvo, getSCMUIContext());
  }

  public void afterSetBillVOToCard(AggregatedValueObject billvo)
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.afterSetBillVOToCard(billvo, getSCMUIContext());
  }

  public void beforeSetBillVOsToListHead(CircularlyAccessibleValueObject[] headvos)
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.beforeSetBillVOsToListHead(headvos, getSCMUIContext());
  }

  public void afterSetBillVOsToListHead(CircularlyAccessibleValueObject[] headvos)
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.afterSetBillVOsToListHead(headvos, getSCMUIContext());
  }

  public void beforeSetBillVOsToListBody(CircularlyAccessibleValueObject[] bodyvos)
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.beforeSetBillVOsToListBody(bodyvos, getSCMUIContext());
  }

  public void afterSetBillVOsToListBody(CircularlyAccessibleValueObject[] bodyvos)
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.afterSetBillVOsToListBody(bodyvos, getSCMUIContext());
  }

  public void onAddLine()
    throws BusinessException
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.onAddLine(getSCMUIContext());
  }

  public void onPastLine()
    throws BusinessException
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return;
    for (IScmUIPlugin pl : this.listPlugin)
      pl.onPastLine(getSCMUIContext());
  }

  public String onQuery(String swhere)
    throws BusinessException
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return swhere;
    for (IScmUIPlugin pl : this.listPlugin) {
      swhere = QueryDlgUtil.andTowWhere(swhere, pl.onQuery(swhere, getSCMUIContext()));
    }
    return swhere;
  }

  public AggregatedValueObject[] retChangeBusiVOs(AggregatedValueObject[] preVo, AggregatedValueObject[] nowVo)
    throws BusinessException
  {
    init();

    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return nowVo;
    AggregatedValueObject[] retVos = nowVo;
    AggregatedValueObject[] tempretVos = null;
    for (IScmUIPlugin pl : this.listPlugin) {
      tempretVos = pl.retChangeBusiVOs(preVo, nowVo);
      if ((null != tempretVos) && (0 < tempretVos.length))
        retVos = tempretVos;
    }
    return retVos;
  }

  public Object[] retBillToBillRefVOs(CircularlyAccessibleValueObject[] headVos, CircularlyAccessibleValueObject[] bodyVos)
    throws BusinessException
  {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return null;
    Object[] retVos = null;
    Object[] tempretVos = null;
    for (IScmUIPlugin pl : this.listPlugin) {
      tempretVos = pl.retBillToBillRefVOs(headVos, bodyVos);
      if ((null != tempretVos) && (0 < tempretVos.length))
        retVos = tempretVos;
    }
    return retVos;
  }

  public String getUiclassname() {
    return this.uiclassname;
  }

  public void setUiclassname(String uiclassname) {
    this.uiclassname = uiclassname;
  }

  public AggregatedValueObject[] beforePrint(AggregatedValueObject[] printVOs) {
    init();
    if ((this.listPlugin == null) || (this.listPlugin.size() <= 0))
      return null;
    AggregatedValueObject[] retVos = printVOs;
    AggregatedValueObject[] tempretVOs = null;
    for (IScmUIPlugin pl : this.listPlugin) {
      tempretVOs = pl.beforePrint(printVOs, getSCMUIContext());
      if (null != tempretVOs)
        retVos = tempretVOs;
    }
    return retVos;
  }
}